package org.ipccenter.balancechecker.xmlconf;
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.ipccenter.balancechecker.jaxb.Configuration;
import org.ipccenter.balancechecker.listener.Listener;
import org.ipccenter.balancechecker.services.MiptTelecom;
import org.ipccenter.balancechecker.services.Service1;
import org.ipccenter.balancechecker.services.ServiceType;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import java.util.*;

public class XMLReader {

    /**
     * The main method of XMLReader class, it's responsible for parsing XML file
     * (configuration.xml by default) that contains name of the service, username and
     * password.
     * @param fileName Name of the file to parse
     */

    public List<ServiceType> parseXML(String fileName) {

        File f;
        if( fileName == null )
            f = new File("configuration.xml");
        else
            f = new File(fileName);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(true);
        final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
        final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
        factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", "file:./configuration.xsd");

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            builder.setErrorHandler( new SimpleErrorHandler());
            Document doc = builder.parse(f);
        } catch (ParserConfigurationException e) {
            System.out.println(" DocumentBuilder cannot be created which satisfies the configuration requested\n"
                    + e.getStackTrace());
        } catch(IOException e1) {
            System.out.println("IO error occured while parsing the file\n"
                    + e1.getStackTrace());
        } catch(SAXException e2) {
            System.out.println("Parse error occured while parsing the file\n"
                    + e2.getStackTrace());
        }


        Configuration item = null;
        try{
            JAXBContext context = JAXBContext.newInstance(Configuration.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            File file = new File ("configuration.xml");
            item = (Configuration) unmarshaller.unmarshal(file);
        } catch(JAXBException e) {
            System.out.println("Some unexpected error occured while unmarshalling the file\n"
                    + e.getStackTrace());
        }

        ArrayList<ServiceType> list = (ArrayList<ServiceType>)item.getServices();
        return list;
    }

    /**
     * Testing method
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /**
        XMLReader tester = new XMLReader();
        List<ServiceType> list = new ArrayList<ServiceType>();
        List<String> list2 = new ArrayList<String>();
        list = tester.parseXML("configuration.xml");
        System.out.println(list.get(0).toString());
        MiptTelecom mip = new MiptTelecom();
        mip = (MiptTelecom) list.get(0);
        Service1 mip2 = new Service1();
        mip2 = (Service1) list.get(2);
        Listener listen = mip2.getListener();
        list2 = listen.actionList;
        System.out.println(listen.minBalance);
        */
    }

}
